﻿using System.Windows;
using System.Windows.Media;
using _3DTicTacToe.Core;
using _3DTicTacToe.Net;

namespace _3DTicTacToe
{
    public partial class NetJoinPropertiesWindow : Window
    {
        private bool _playerPropertiesSelected;
        private Player _player;
        private bool _showStartWindow = true;

        public NetJoinPropertiesWindow()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!_playerPropertiesSelected)
            {
                MessageBox.Show("Please choose player properties", "Choose player!", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }

            _showStartWindow = false;

            var connection = TConnection.CreateClient(hostTextBox.Text, int.Parse(portTextBox.Text));
            connection.Start();

            new ClientConnectingWindow(connection, _player, hostTextBox.Text + ":" + portTextBox.Text).Show();
            Close();
        }

        private void changeButton_Click(object sender, RoutedEventArgs e)
        {
            var playerPropWin = new PlayerPropertiesWindow{Owner = this};
            var result = playerPropWin.ShowDialog();

            if (!result.HasValue || !result.Value) return;

            border.Background = new SolidColorBrush(playerPropWin.Player.Color);
            content.Foreground = new SolidColorBrush(GameWindow.InvertColor(playerPropWin.Player.Color));
            content.Content = playerPropWin.Player.Name;

            _player = playerPropWin.Player;
            _playerPropertiesSelected = true;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_showStartWindow)
                new StartWindow().Show();
        }
    }
}
